package xyz.zdsoft.common.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class JwtUtils {

    //常量
    public static final long EXPIRE = 1000 * 60 * 60 * 24; //token过期时间
//        public static long EXPIRE = 1000 * 60 * 2 ; //token过期时间
    public static final String APP_SECRET = "tyh8BDbRigUDaY6pZJfWus1jZWLTJC"; //秘钥

    //暂时用于数据交换
/*    public static String Id = "123456";
    public static String UserType = "student";*/
    //生成token字符串的方法
    public static String getJwtToken(String id, String userType) {

        //因为还没有与前端交互，所以不能将token放到请求头中，我们在这里设置一个假token
  /*      id = Id;
        userType = UserType;*/
        String JwtToken = Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .claim("id", id)  //设置token主体部分 ，存储用户信息
                .claim("userType", userType)
                .signWith(SignatureAlgorithm.HS256, APP_SECRET)
                .compact();
        return JwtToken;
    }

    //生成token字符串的方法
    public static String getJwtCheckToken(String checkCode) {
        String JwtToken = Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .claim("checkCode", checkCode)  //设置token主体部分 ，存储用户信息
                .signWith(SignatureAlgorithm.HS256, APP_SECRET)
                .compact();
        return JwtToken;
    }


    public static boolean checkCookie(String token) {
        if (StringUtils.hasLength(token)) {
            try {
                Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token);
                return true;
            } catch (Exception e) {
//                e.printStackTrace();
                System.out.println("无效的验证码Id或用户token已失效");
            }
        }
        return false;
    }

    public static String getMemberIdByJwtToken(String token) {
//        token = getJwtToken(Id,UserType);
        if (token == null || token.equals("")) return "";
        Claims claims = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token).getBody();
        return (String) claims.get("id");
    }

    public static String getMemberCheckCodeByJwtToken(String token) {
        if (token == null || token.equals("")) return "";
        Claims claims = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token).getBody();
        return (String) claims.get("checkCode");
    }

    public static String getMemberUserTypeByJwtToken(String token) {
//        token = getJwtToken(Id,UserType);
        if (token == null || token.equals("")) return "";
        Claims claims = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token).getBody();
        return (String) claims.get("userType");
    }

    public static String getToken(HttpServletRequest request) {
        return request.getHeader("Authorization");
    }

    /**
     * 直接通过这个方法得到存储在token中的UserId
     * @param request
     * @return
     */
    public static String getMemberIdByJwtToken(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
//        token = getJwtToken(Id,UserType);
        if (token == null || token.equals("")) return "";
        Claims claims = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token).getBody();
        return (String) claims.get("id");
    }

    /**
     * 直接通过这个方法得到存储在token中的UserType
     * @param request
     * @return
     */
    public static String getMemberUserTypeByJwtToken(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
//        token = getJwtToken(Id,UserType);
        if (token == null || token.equals("")) return "";
        Claims claims = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token).getBody();
        return (String) claims.get("userType");
    }
}